IF EXISTS (
	SELECT SPECIFIC_NAME 
	FROM INFORMATION_SCHEMA.ROUTINES 
	WHERE SPECIFIC_SCHEMA = N'testing'
		AND SPECIFIC_NAME = N'prc_InsertComment' 
)
	BEGIN
		DROP PROCEDURE testing.prc_InsertComment
		PRINT 'Dropping testing.prc_InsertComment'
	END
GO

/*-------------------------------------------------------------------------------------
** Name:	prc_InsertComment
** Purpose:	insert comment
** 
** ------------------------------------------------------------------------------------
** Input Parameters: 
** @QuestionGroupId
** @QuestionId
** @Content
** @CreateBy
** 
** ------------------------------------------------------------------------------------
** Usage
** EXEC testing.prc_InsertComment 1, 1, 'test', 'admin'
**
** ------------------------------------------------------------------------------------
** Author:    Hau
** Date:      2012-07-17
** Database:  SIVN_OnlineTestingEngine
**
** ------------------------------------------------------------------------------------
** Modifications:
** 2012-07-17	Hau		Create
** ----------------------------------------------------------------------------------*/

CREATE PROCEDURE [testing].[prc_InsertComment]
@QuestionGroupId int,
@QuestionDetailId int = null,
@Content nvarchar(1000),
@CreateBy nvarchar(100)
AS
BEGIN
	SET NOCOUNT ON;
	
	declare @QuestionId int
	
	if @QuestionDetailId is not null and @QuestionGroupId is null
		select @QuestionId = QuestionId from testing.[QuestionGroupDetail]
		where Id = @QuestionDetailId
	
	Insert into testing.Comment(Content, QuestionId, QuestionGroupId, CreatedDate, CreatedBy, ModifiedDate, ModifiedBy)
	values (@Content, @QuestionId, @QuestionGroupId, GETDATE(), @CreateBy, GETDATE(), @CreateBy)
END

PRINT 'Created [testing].[prc_InsertComment]'
GO